package com.dayanxs.Controller;

import com.dayanxs.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/user")
public class UserController {

    private final UserService userService;
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/login")
    public ResponseEntity<?> login(
            @RequestBody Map<String, String> userLogin
    ) {
        return userService.handleLogin(userLogin.get("username"), userLogin.get("password"));
    }

    @GetMapping("/info")
    public ResponseEntity<?> getUserAndNovelInfo(
            @RequestHeader(value = "Authorization", required = false) String authorization) {
        return userService.getUserAndNovelInfo(authorization);
    }

    @PostMapping("/register")
    public ResponseEntity<?> register(
            @RequestBody Map<String, String> userInfo
    ) {
        return userService.handleRegistration(userInfo);
    }

    @GetMapping("/delete")
    public ResponseEntity<?> deleteUser(
            @RequestParam int id,
            @RequestHeader(value = "Authorization", required = false) String authorization) {
        return userService.handleDeleteUser(id, authorization);
    }

    @PostMapping("/update")
    public ResponseEntity<?> updateUser(
            @RequestHeader(value = "Authorization", required = false) String authorization,
            @RequestBody Map<String, String> userDetails) {
        return userService.updateUser(authorization, userDetails);
    }

    @PostMapping("/change-password")
    public ResponseEntity<?> changePassword(
            @CookieValue(value = "userToken", defaultValue = "") String userToken,
            @RequestParam Map<String, String> passwords) {
        return userService.changePassword(userToken, passwords);
    }
    @GetMapping("/search")
    public ResponseEntity<?> searchUsers(
            @RequestHeader(value = "Authorization", required = false) String authorization,
            @RequestParam String username,
            @RequestParam(defaultValue = "1") int type) {
        return userService.searchUsers(authorization,username,type);
    }
}
